package com.usian.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.model.article.dtos.ArticleHomeDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
   @Autowired
   private ApArticleMapper apArticleMapper;

    public static final int LOADTYPE_LOAD_MORE = 1; //上拉更多
    public static final int LOADTYPE_LOAD_NEW = 2;  //下拉最新
    public static final String DEFAULT_TAG = "__all__"; //默认频道

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public ResponseResult load(int loadtype, ArticleHomeDto dto) {

        //校验条件 size  type  date
        if(dto.getSize()==null){
            dto.setSize(5);
        }

        if(loadtype!=LOADTYPE_LOAD_MORE &&loadtype!=LOADTYPE_LOAD_NEW){
            loadtype = LOADTYPE_LOAD_MORE;
        }

        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        if(StringUtils.isBlank(dto.getTag())){
            dto.setTag(DEFAULT_TAG);
        }

        List<ApArticle> articleList = apArticleMapper.findList(dto, loadtype);
        ResponseResult responseResult =ResponseResult.okResult(articleList);
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }
}
